//
//  PictureViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "PictureViewController.h"
#import "PictureModel.h"
#import "PictureDetailViewController.h"
#import "PictureViewCell.h"
#import "UIImageView+WebCache.h"
#import "BigImageViewController.h"

@interface PictureViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *modelArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)BOOL isUpLoading;
@end

@implementation PictureViewController

- (NSMutableArray *)modelArray {
    if (!_modelArray) {
        self.modelArray = [NSMutableArray array];
    }
    return [[_modelArray retain] autorelease];
}

-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.898 green:0.063 blue:0.000 alpha:1.000];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    [self initUI];
    //刷新 下载
    [self addHeader];
    [self addFooter];
    

    
    
    
}
-(void)initUI
{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(10 *SCREEN_WIDTH, 0, 355 * SCREEN_WIDTH, 657 * SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
        [self.myTableView registerClass:[PictureViewCell class] forCellReuseIdentifier:@"cell"];

}

-(void)addHeader
{
    
    
    self.myTableView.headerHidden = NO;
    __block PictureViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.page = 0;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.page];

    }];
    [self.myTableView headerBeginRefreshing];

}

-(void)addFooter
{
    __block PictureViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        
        vc.page ++;
        vc.isUpLoading = YES;
        [vc GetPersonList:vc.page];
    }];
}
-(void)GetPersonList:(NSInteger)nextCursor
{
  
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    NSString *getstr = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=list&appname=baisibudejie&asid=64FD4D2D-91F4-4619-8089-29D43611F42B&c=data&client=iphone&device=iPhone%204&from=ios&jbk=1&mac=&market=&openudid=4690a8b3a9cc59296735340a3cdaad43d692fc3f&page=%ld&per=20&type=10&udid=&ver=3.2",self.page];
    
    [mgr GET:getstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.modelArray removeAllObjects];
        }
        NSMutableArray *allArr= [responseObject objectForKey:@"list"];
        
        
        for (NSDictionary *dic in allArr) {
            
            PictureModel *picModel = [[PictureModel alloc]init];
            [picModel setValuesForKeysWithDictionary:dic];
            [self.modelArray addObject:picModel];
        }

        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mb.mode = MBProgressHUDModeText;
        mb.labelText = @"网络不给力";
        [mb hide:YES afterDelay:2];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureModel *model = self.modelArray[indexPath.row];
    PictureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UITapGestureRecognizer *bigTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    cell.picImageView.userInteractionEnabled = YES;
    cell.picImageView.tag = 1000 + indexPath.row;
    [cell.picImageView addGestureRecognizer:bigTap];
    
    [[SDImageCache sharedImageCache] clearMemory];
    return cell;
   
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    BigImageViewController *bigImageVC = [[BigImageViewController
                                           alloc]init];
    bigImageVC.model = self.modelArray[tap.view.tag - 1000];
   
    [self.navigationController pushViewController:bigImageVC animated:YES];
     [self.navigationController setNavigationBarHidden:YES];
}


#pragma 点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureDetailViewController *picDetail = [[PictureDetailViewController alloc]init];
    
    picDetail.model = self.modelArray[indexPath.row];
    [self.navigationController pushViewController:picDetail animated:YES];
//    self.navigationController.navigationBarHidden = YES;
    
 
}

#pragma 行高  
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureModel *picModel = [self.modelArray
                              objectAtIndex:indexPath.row];
    
    CGFloat picWidth = self.view.frame.size.width - 20; // 保证宽度一致
    CGFloat scale = picWidth / picModel.width; // 缩放比例
    CGFloat picHeight = (CGFloat)picModel.height * scale; // 保证比例不失调
    if (picHeight > 667.0) {
        picHeight = 300;
    }
    
    return picHeight + [PictureViewCell heightForText:picModel.text width:self.view.frame.size.width - 40] + 60 + 20;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
